package com.solcoo.util;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.FileOutputStream;

import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.ui.TextAnchor;

public class ReportPicUtil {
	
	/**
	 * 生成 3D 柱状图
	 */
	public static void createBarCharts3D(JFreeChart chart, String picPath, String picName) {
		TextTitle tt = chart.getTitle();
		
		tt.setFont(new Font("SimSun", Font.ITALIC, 16));
		tt.setPaint(Color.RED);
		
		chart.setBorderPaint(Color.decode("#CCCCCC")); // 形边框颜色 
		chart.setBorderStroke(new BasicStroke(1.0f)); // 图形边框粗细
		chart.setBorderVisible(true); // 可见
		
		chart.getLegend().setItemFont(new Font("SimSun", Font.PLAIN, 14));
		
		CategoryPlot plot = (CategoryPlot) chart.getPlot(); // 获得图表区域对象
		
		plot.setRangeGridlinePaint(Color.BLUE); // 横网格线蓝色
		plot.setRangeGridlinesVisible(true); // 可见
	       
		plot.setDomainGridlinePaint(Color.BLUE); // 纵网格线蓝色
		plot.setDomainGridlinesVisible(false); // 不可见
		
		plot.setBackgroundPaint(Color.decode("#F0F8FF")); // 设置网格背景颜色
		plot.setForegroundAlpha(0.8f); // 设置住的透明度
		
		// X 轴
		CategoryAxis x = plot.getDomainAxis();
		
		x.setAxisLineStroke(new BasicStroke(1.0f)); // X 轴粗细
	    x.setAxisLinePaint(Color.BLUE); // X 轴颜色
	    x.setAxisLineVisible(true); // X 轴可见
		
	    // X 轴标签
	    x.setLabelFont(new Font("SimSun", Font.PLAIN, 12));
	    x.setLabelPaint(Color.BLUE);
	    x.setCategoryLabelPositions(CategoryLabelPositions.STANDARD);
	    
	    // X 轴刻度标签
	    x.setTickLabelFont(new Font("SimSun", Font.PLAIN, 10));
	    x.setTickLabelPaint(Color.RED);
	    x.setTickLabelsVisible(true);
	    
	    // X 轴刻度
	    x.setTickMarkStroke(new BasicStroke(1.0f)); // X 轴刻度粗细
	    x.setTickMarkPaint(Color.RED); // X 轴刻度颜色
	    x.setTickMarksVisible(true); // X 轴刻度是否显示
	    
		// Y 轴
		NumberAxis y = (NumberAxis) plot.getRangeAxis();
		
		y.setAxisLineStroke(new BasicStroke(1.0f)); // Y 轴粗细
		y.setAxisLinePaint(Color.BLUE); // Y 轴颜色
		y.setAxisLineVisible(true); // Y 轴可见
		
		// Y 轴标签
		y.setLabelFont(new Font("SimSun", Font.PLAIN, 12));
	    y.setLabelPaint(Color.BLUE);
	    
	    // Y 轴刻度标签
	    y.setTickLabelFont(new Font("SimSun", Font.PLAIN, 12));
	    y.setTickLabelPaint(Color.BLUE);
	    y.setTickLabelsVisible(true);
	    
	    // Y 轴刻度
	    y.setTickMarkStroke(new BasicStroke(1.0f)); // Y 轴刻度粗细
	    y.setTickMarkPaint(Color.RED); // Y 轴刻度颜色
	    y.setTickMarksVisible(true); // Y 轴刻度是否显示
	    
	    y.setUpperMargin(0.1); // 设置距离图片顶端距离此时为 10%
		
		BarRenderer3D renderer = new BarRenderer3D();
		
		// 设置柱子的颜色
		renderer.setSeriesPaint(0, Color.decode("#6495ED"));

		// 边框为蓝色
		renderer.setSeriesOutlinePaint(0, Color.BLUE);
		renderer.setSeriesOutlineStroke(0, new BasicStroke(2.0f));
		
		renderer.setMaximumBarWidth(0.01); // 柱的最大宽度
		
		renderer.setBaseLegendTextFont(new Font("SimSun", Font.PLAIN, 10));
		renderer.setBaseLegendTextPaint(Color.GRAY);
		
		// 显示每个柱的数值
        renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        renderer.setBaseItemLabelsVisible(true);
        renderer.setBaseItemLabelFont(new Font("SimSun", Font.PLAIN, 12));
        renderer.setBaseItemLabelPaint(Color.RED);
        
        // 设置柱形图上的文字偏离值
        renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER)); 
        renderer.setItemLabelAnchorOffset(10D);
        
		plot.setRenderer(renderer); // 使用我们设计的效果
		
		try {
			File filePath = new File(picPath);
			
			if ( !filePath.exists() )
				filePath.mkdirs();
			
			FileOutputStream fos = new FileOutputStream(picPath + picName);
		       
			ChartUtilities.writeChartAsJPEG(fos, 1.0f, chart, 800, 400, null);

			fos.close();
		} catch (Exception e) {}
	}
	
	/**
	 * 生成折线图
	 */
	public static void createLineCharts(JFreeChart chart, String picPath, String picName) {
		TextTitle tt = chart.getTitle();
		
		tt.setFont(new Font("宋体", Font.ITALIC, 16));
		tt.setPaint(Color.RED);
		
		chart.setBorderPaint(Color.decode("#CCCCCC")); // 形边框颜色 
		chart.setBorderStroke(new BasicStroke(1.0f)); // 图形边框粗细
		chart.setBorderVisible(true); // 可见
		
		chart.getLegend().setItemFont(new Font("SimSun", Font.PLAIN, 14));
		
		CategoryPlot plot = (CategoryPlot) chart.getPlot(); // 获得图表区域对象
		
		plot.setRangeGridlinePaint(Color.BLUE); // 横网格线蓝色
		plot.setRangeGridlinesVisible(true); // 可见
	       
		plot.setDomainGridlinePaint(Color.BLUE); // 纵网格线蓝色
		plot.setDomainGridlinesVisible(true); // 可见
		
		plot.setBackgroundPaint(Color.decode("#F0F8FF")); // 设置网格背景颜色
		plot.setForegroundAlpha(0.8f); // 设置住的透明度
		
		// X 轴
		CategoryAxis x = plot.getDomainAxis();
		
		x.setAxisLineStroke(new BasicStroke(1.0f)); // X 轴粗细
	    x.setAxisLinePaint(Color.BLUE); // X 轴颜色
	    x.setAxisLineVisible(true); // X 轴可见
		
	    // X 轴标签
	    x.setLabelFont(new Font("SimSun", Font.PLAIN, 12));
	    x.setLabelPaint(Color.BLUE);
	    x.setCategoryLabelPositions(CategoryLabelPositions.STANDARD);
	    
	    // X 轴刻度标签
	    x.setTickLabelFont(new Font("SimSun", Font.PLAIN, 10));
	    x.setTickLabelPaint(Color.RED);
	    x.setTickLabelsVisible(true);
	    
	    // X 轴刻度
	    x.setTickMarkStroke(new BasicStroke(1.0f)); // X 轴刻度粗细
	    x.setTickMarkPaint(Color.RED); // X 轴刻度颜色
	    x.setTickMarksVisible(true); // X 轴刻度是否显示
	    
		// Y 轴
		NumberAxis y = (NumberAxis) plot.getRangeAxis();
		
		y.setAxisLineStroke(new BasicStroke(1.0f)); // Y 轴粗细
		y.setAxisLinePaint(Color.BLUE); // Y 轴颜色
		y.setAxisLineVisible(true); // Y 轴可见
		
		// Y 轴标签
		y.setLabelFont(new Font("SimSun", Font.PLAIN, 12));
	    y.setLabelPaint(Color.BLUE);
	    
	    // Y 轴刻度标签
	    y.setTickLabelFont(new Font("SimSun", Font.PLAIN, 10));
	    y.setTickLabelPaint(Color.BLUE);
	    y.setTickLabelsVisible(true);
	    
	    // Y 轴刻度
	    y.setTickMarkStroke(new BasicStroke(1.0f)); // Y 轴刻度粗细
	    y.setTickMarkPaint(Color.RED); // Y 轴刻度颜色
	    y.setTickMarksVisible(true); // Y 轴刻度是否显示
	    
	    y.setUpperMargin(0.1); // 设置距离图片顶端距离此时为 10%
	    
	    LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
	    
	    renderer.setBaseShapesVisible(true); // 设置拐点是否可见/是否显示拐点
	    renderer.setDrawOutlines(true); // 设置拐点不同用不同的形状

	    // 设置折线加粗
	    renderer.setSeriesStroke(0, new BasicStroke(1F));
	    renderer.setSeriesOutlineStroke(0, new BasicStroke(2.0F));
	    
	    // 显示折点数据
	    renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
	    renderer.setBaseItemLabelsVisible(true);
	    renderer.setBaseItemLabelPaint(Color.BLUE);
	    renderer.setBaseItemLabelFont(new Font("SimSun", Font.PLAIN, 18));
	    renderer.setItemMargin(0.1); // 间隔
		   
	    renderer.setBaseLegendTextPaint(Color.BLUE);
	    
	    try {
			File filePath = new File(picPath);
			
			if ( !filePath.exists() )
				filePath.mkdirs();
			
			FileOutputStream fos = new FileOutputStream(picPath + picName);
		       
			ChartUtilities.writeChartAsJPEG(fos, 1.0f, chart, 800, 380, null);

			fos.close();
		} catch (Exception e) {}
	}
	
	/**
	 * 生成折线图
	 */
	public static void createLineCharts30(JFreeChart chart, String picPath, String picName) {
		TextTitle tt = chart.getTitle();
		
		tt.setFont(new Font("宋体", Font.ITALIC, 16));
		tt.setPaint(Color.RED);
		
		chart.setBorderPaint(Color.decode("#CCCCCC")); // 形边框颜色 
		chart.setBorderStroke(new BasicStroke(1.0f)); // 图形边框粗细
		chart.setBorderVisible(true); // 可见
		
		chart.getLegend().setItemFont(new Font("SimSun", Font.PLAIN, 14));
		
		CategoryPlot plot = (CategoryPlot) chart.getPlot(); // 获得图表区域对象
		
		plot.setRangeGridlinePaint(Color.BLUE); // 横网格线蓝色
		plot.setRangeGridlinesVisible(true); // 可见
	       
		plot.setDomainGridlinePaint(Color.BLUE); // 纵网格线蓝色
		plot.setDomainGridlinesVisible(true); // 可见
		
		plot.setBackgroundPaint(Color.decode("#F0F8FF")); // 设置网格背景颜色
		plot.setForegroundAlpha(0.8f); // 设置住的透明度
		
		// X 轴
		CategoryAxis x = plot.getDomainAxis();
		
		x.setAxisLineStroke(new BasicStroke(1.0f)); // X 轴粗细
	    x.setAxisLinePaint(Color.BLUE); // X 轴颜色
	    x.setAxisLineVisible(true); // X 轴可见
		
	    // X 轴标签
	    x.setLabelFont(new Font("SimSun", Font.PLAIN, 12));
	    x.setLabelPaint(Color.BLUE);
	    x.setCategoryLabelPositions(CategoryLabelPositions.STANDARD);
	    
	    // X 轴刻度标签
	    x.setTickLabelFont(new Font("SimSun", Font.PLAIN, 12));
	    x.setTickLabelPaint(Color.RED);
	    x.setTickLabelsVisible(true);
	    
	    // X 轴刻度
	    x.setTickMarkStroke(new BasicStroke(1.0f)); // X 轴刻度粗细
	    x.setTickMarkPaint(Color.RED); // X 轴刻度颜色
	    x.setTickMarksVisible(true); // X 轴刻度是否显示
	    
	    x.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
	    
		// Y 轴
		NumberAxis y = (NumberAxis) plot.getRangeAxis();
		
		y.setAxisLineStroke(new BasicStroke(1.0f)); // Y 轴粗细
		y.setAxisLinePaint(Color.BLUE); // Y 轴颜色
		y.setAxisLineVisible(true); // Y 轴可见
		
		// Y 轴标签
		y.setLabelFont(new Font("SimSun", Font.PLAIN, 12));
	    y.setLabelPaint(Color.BLUE);
	    
	    // Y 轴刻度标签
	    y.setTickLabelFont(new Font("SimSun", Font.PLAIN, 10));
	    y.setTickLabelPaint(Color.BLUE);
	    y.setTickLabelsVisible(true);
	    
	    // Y 轴刻度
	    y.setTickMarkStroke(new BasicStroke(1.0f)); // Y 轴刻度粗细
	    y.setTickMarkPaint(Color.RED); // Y 轴刻度颜色
	    y.setTickMarksVisible(true); // Y 轴刻度是否显示
	    
	    y.setUpperMargin(0.1); // 设置距离图片顶端距离此时为 10%
	    
	    LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
	    
	    renderer.setBaseShapesVisible(true); // 设置拐点是否可见/是否显示拐点
	    renderer.setDrawOutlines(true); // 设置拐点不同用不同的形状

	    // 设置折线加粗
	    renderer.setSeriesStroke(0, new BasicStroke(1F));
	    renderer.setSeriesOutlineStroke(0, new BasicStroke(2.0F));
	    
	    // 显示折点数据
	    renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
	    renderer.setBaseItemLabelsVisible(true);
	    renderer.setBaseItemLabelPaint(Color.BLUE);
	    renderer.setBaseItemLabelFont(new Font("SimSun", Font.PLAIN, 18));
	    renderer.setItemMargin(0.1); // 间隔
		   
	    renderer.setBaseLegendTextPaint(Color.BLUE);
	    
	    try {
			File filePath = new File(picPath);
			
			if ( !filePath.exists() )
				filePath.mkdirs();
			
			FileOutputStream fos = new FileOutputStream(picPath + picName);
		       
			ChartUtilities.writeChartAsJPEG(fos, 1.0f, chart, 1000, 380, null);

			fos.close();
		} catch (Exception e) {}
	}
	
	/**
	 * 生成折线图
	 */
	public static void createLineCharts30ByShangHu(JFreeChart chart, String picPath, String picName) {
		TextTitle tt = chart.getTitle();
		
		tt.setFont(new Font("宋体", Font.ITALIC, 16));
		tt.setPaint(Color.RED);
		
		chart.setBorderPaint(Color.decode("#CCCCCC")); // 形边框颜色 
		chart.setBorderStroke(new BasicStroke(1.0f)); // 图形边框粗细
		chart.setBorderVisible(true); // 可见
		
		chart.getLegend().setItemFont(new Font("SimSun", Font.PLAIN, 14));
		
		CategoryPlot plot = (CategoryPlot) chart.getPlot(); // 获得图表区域对象
		
		plot.setRangeGridlinePaint(Color.BLUE); // 横网格线蓝色
		plot.setRangeGridlinesVisible(true); // 可见
	       
		plot.setDomainGridlinePaint(Color.BLUE); // 纵网格线蓝色
		plot.setDomainGridlinesVisible(true); // 可见
		
		plot.setBackgroundPaint(Color.decode("#F0F8FF")); // 设置网格背景颜色
		plot.setForegroundAlpha(0.8f); // 设置住的透明度
		
		// X 轴
		CategoryAxis x = plot.getDomainAxis();
		
		x.setAxisLineStroke(new BasicStroke(1.0f)); // X 轴粗细
	    x.setAxisLinePaint(Color.BLUE); // X 轴颜色
	    x.setAxisLineVisible(true); // X 轴可见
		
	    // X 轴标签
	    x.setLabelFont(new Font("SimSun", Font.PLAIN, 12));
	    x.setLabelPaint(Color.BLUE);
	    x.setCategoryLabelPositions(CategoryLabelPositions.STANDARD);
	    
	    // X 轴刻度标签
	    x.setTickLabelFont(new Font("SimSun", Font.PLAIN, 12));
	    x.setTickLabelPaint(Color.RED);
	    x.setTickLabelsVisible(true);
	    
	    // X 轴刻度
	    x.setTickMarkStroke(new BasicStroke(1.0f)); // X 轴刻度粗细
	    x.setTickMarkPaint(Color.RED); // X 轴刻度颜色
	    x.setTickMarksVisible(true); // X 轴刻度是否显示
	    
	    x.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
	    
		// Y 轴
		NumberAxis y = (NumberAxis) plot.getRangeAxis();
		
		y.setAxisLineStroke(new BasicStroke(1.0f)); // Y 轴粗细
		y.setAxisLinePaint(Color.BLUE); // Y 轴颜色
		y.setAxisLineVisible(true); // Y 轴可见
		
		// Y 轴标签
		y.setLabelFont(new Font("SimSun", Font.PLAIN, 12));
	    y.setLabelPaint(Color.BLUE);
	    
	    // Y 轴刻度标签
	    y.setTickLabelFont(new Font("SimSun", Font.PLAIN, 10));
	    y.setTickLabelPaint(Color.BLUE);
	    y.setTickLabelsVisible(true);
	    
	    // Y 轴刻度
	    y.setTickMarkStroke(new BasicStroke(1.0f)); // Y 轴刻度粗细
	    y.setTickMarkPaint(Color.RED); // Y 轴刻度颜色
	    y.setTickMarksVisible(true); // Y 轴刻度是否显示
	    
	    y.setUpperMargin(0.1); // 设置距离图片顶端距离此时为 10%
	    
	    LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
	    
	    renderer.setBaseShapesVisible(true); // 设置拐点是否可见/是否显示拐点
	    renderer.setDrawOutlines(true); // 设置拐点不同用不同的形状

	    // 设置折线加粗
	    renderer.setSeriesStroke(0, new BasicStroke(1F));
	    renderer.setSeriesOutlineStroke(0, new BasicStroke(2.0F));
	    
	    // 显示折点数据
	    renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
	    renderer.setBaseItemLabelsVisible(true);
	    renderer.setBaseItemLabelPaint(Color.BLUE);
	    renderer.setBaseItemLabelFont(new Font("SimSun", Font.PLAIN, 18));
	    renderer.setItemMargin(0.1); // 间隔
		   
	    renderer.setBaseLegendTextPaint(Color.BLUE);
	    
	    try {
			File filePath = new File(picPath);
			
			if ( !filePath.exists() )
				filePath.mkdirs();
			
			FileOutputStream fos = new FileOutputStream(picPath + picName);
		       
			ChartUtilities.writeChartAsJPEG(fos, 1.0f, chart, 880, 380, null);

			fos.close();
		} catch (Exception e) {}
	}
	
}